﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Controls
{
	public class NumericStringHierarchicalSearchItem<HierarchicalControlType> : SearchItem
	{
		private System.Windows.FrameworkElement _SimpleSearcherInput;
		public override System.Windows.FrameworkElement SimpleSearcherInput
		{
			get
			{
				if (this._SimpleSearcherInput == null)
					this._SimpleSearcherInput = (System.Windows.FrameworkElement)System.Activator.CreateInstance(typeof(HierarchicalControlType));
				return this._SimpleSearcherInput;
			}
		}

		public bool AllowNull
		{
			get
			{
				return (bool)this.SimpleSearcherInput.GetType().GetProperty("AllowNull").GetValue(this.SimpleSearcherInput, null);
			}
			set
			{
				this.SimpleSearcherInput.GetType().GetProperty("AllowNull").SetValue(this.SimpleSearcherInput, value, null);
			}
		}

		/*
	{ Text: "包含", Value: "{0}.Contains({1})" },
	{ Text: "不包含", Value: "!{0}.Contains({1})" },
	{ Text: "等于", Value: "{0}=={1}" },
	{ Text: "始于", Value: "{0}.StartsWith({1})" },
	{ Text: "非始于", Value: "!{0}.StartsWith({1})" },
	{ Text: "终于", Value: "{0}.EndsWith({1})" },
	{ Text: "非终于", Value: "!{0}.EndsWith({1})" }
		 */
		private Operator[] _OperatorOptions;
		public override Operator[] Operators
		{
			get
			{
				if (this._OperatorOptions == null)
					this._OperatorOptions = new Operator[]
					{
						new Operator("=","Contains"),
						new Operator("≠","!Contains")
					};
				return this._OperatorOptions;
			}
		}

		public override System.Windows.FrameworkElement AdvanceSearcherInput
		{
			get
			{
				System.Windows.FrameworkElement selector = (System.Windows.FrameworkElement)System.Activator.CreateInstance(typeof(HierarchicalControlType));
				selector.GetType().GetProperty("AllowNull").SetValue(selector, this.AllowNull, null);

				return selector;
			}
		}

		public override string InputValueProperty
		{
			get
			{
				return "SelectedId";
			}
		}

		public override Sunny.Controls.Searcher.WhereCommand GetCommand(string field, string expression, int index, System.Windows.FrameworkElement searcherInput)
		{
			System.Collections.ArrayList parameters = new System.Collections.ArrayList();
			string command = null;
			foreach (int id in (int[])searcherInput.GetType().GetProperty("ResideIdsOfSelectedItem").GetValue(searcherInput, null))
			{
				if (command != null)
					command += "||";
				command += string.Format("{0}.{1}(@{2})", this.Field, expression, index++);
				parameters.Add("," + id + ",");
			}
			return new Sunny.Controls.Searcher.WhereCommand { Text = "(" + command + ")", Parameters = parameters.ToArray() };
		}

		public override Sunny.Controls.Searcher.WhereCommand GetCommand(string field, int index)
		{
			if (this.SimpleSearcherInput == null)
				return null;
			int? selectedId = (int?)this.SimpleSearcherInput.GetType().GetProperty("SelectedId").GetValue(this.SimpleSearcherInput, null);
			if (selectedId == null)
				return null;

			string command = null;
			System.Collections.ArrayList parameters = new System.Collections.ArrayList();
			foreach (int id in (int[])this.SimpleSearcherInput.GetType().GetProperty("ResideIdsOfSelectedItem").GetValue(this.SimpleSearcherInput, null))
			{
				if (command != null)
					command += "||";
				command += string.Format("{0}.Contains(@{1})", field, index++);
				parameters.Add("," + id + ",");
			}
			return new Sunny.Controls.Searcher.WhereCommand { Text = "(" + command + ")", Parameters = parameters.ToArray() };
		}

		public override void Empty()
		{
			if (this._SimpleSearcherInput != null)
				this.SimpleSearcherInput.GetType().GetProperty("SelectedId").SetValue(this._SimpleSearcherInput, null, null);
		}
	}
}